
indexModule.controller('webList',function($scope,$http,$modal){

    $scope.page = {
        page: 1,//当前页数
        limit: 10,// 每页显示多少条
        maxSize: 7 // 显示多少个分页按钮
    };

    //请求用户list
    var postData = {page:$scope.page.page,size:$scope.page.limit};
    userlist(postData,$scope.page.page);
    function userlist(postData,page){
        postHttp($http,config.webList_httpUrl,postData,function(data){
            $scope.tableData = data.data.list;
            $scope.page.page = page;
            $scope.page.total = data.data.total;// list总数量
            $scope.page.numPages = Math.ceil(data.data.total/$scope.page.limit); // 总页数， 101/20 =6, 这个分页那个自动计算的值。
        });
    };

    //页码改变时
    $scope.pageChanged = function(){
        if(typeof(localStorage) === "object"){
            localStorage.setItem("limit",$scope.page.limit);
        };
        if(parseInt($scope.page.page) > parseInt($scope.page.numPages)){
            $scope.page.page = parseInt($scope.page.numPages);
        }else if(parseInt($scope.page.page) < 1){
            $scope.page.page = 1;
        }
        var postData = {page:$scope.page.page,size:$scope.page.limit};
        userlist(postData,$scope.page.page);
    };

    //新增网站
    $scope.add_list = function(){
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: '/views/web/save.html',
            controller: 'saveWebModal',
            resolve: {
                items: function () {
                    return 0;
                }
            }
        });
    };

    //修改网站
    $scope.modify = function(index){
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: '/views/web/save.html',
            controller: 'saveWebModal',
            resolve: {
                items: function () {
                    return $scope.tableData[index];
                }
            }
        });
    };

    //删除网站
    $scope.delete = function(id){
        var postData = {id:id};
        postHttp($http,config.webDel_httpUrl,postData,function(data){
            alert(config.delHttpMsg);
            window.location.reload();
        });
    };

    //点击管理广告，保存该网站信息到cookie，用作下个页面的标题等内容
    $scope.enterAd = function(index){
        $.cookie("webName",$scope.tableData[index].name);
        $.cookie("webUrl",$scope.tableData[index].domain);
    }
});


indexModule.controller('saveWebModal', function ($scope,$http,$modalInstance,items) {
    if(items != 0){
        $scope.name = items.name;
        $scope.domain = items.domain;
    };
    $scope.ok = function () {
        //提交添加
        var name = $scope.name;
        var domain = $scope.domain;
        if(name === 0 && domain === undefined){ //新增网站
            $scope.modify_tips = config.modifyWebTipHttpMsg;
        }else{
            $scope.add_tips = config.saveTipHttpMsg;
            if(items._id){
                var postData = {name:name,domain:domain,id:items._id};
            }else{
                var postData = {name:name,domain:domain};
            };
            postHttp($http,config.webSave_httpUrl,postData,function(data){
                alert(config.addHttpMsg);
                $modalInstance.close('ok');
                window.location.reload();
            });
        }
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});